/**
 * Project Name:fire-industry-DM-center
 * File Name:ElasticsearchConfig.java
 * Package Name:com.firestone.config.elasticsearch
 * Date:2018年3月13日下午2:18:10
 *
*/

package com.firestone.config.elasticsearch;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lease.Releasable;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.util.ReflectionUtils;

/**
 * @Description: TODO(ES client连接)
 * @author CAIYJ
 * @date 2018年3月13日 下午2:18:10
 */
@Configuration
@ConditionalOnClass({Client.class, TransportClientFactoryBean.class})
@EnableConfigurationProperties({ElasticsearchProperties.class})
public class ElasticsearchConfig implements DisposableBean {
    private static final Logger logger = LogManager
            .getLogger(ElasticsearchConfig.class);
    private final ElasticsearchProperties properties;
    private Releasable releasable;

    public ElasticsearchConfig(ElasticsearchProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public Client elasticsearchClient() {
        try {
            return createTransportClient();
        } catch (Exception var2) {
            throw new IllegalStateException(var2);
        }
    }

    @Bean
    public ElasticsearchTemplate elasticsearchTemplate(Client client){
        return new ElasticsearchTemplate(client);
    }

    private Client createTransportClient() throws Exception {
        TransportClientFactoryBean factory = new TransportClientFactoryBean();
        factory.setClusterNodes(this.properties.getClusterNodes());
        factory.setClusterName(this.properties.getClusterName());
        factory.afterPropertiesSet();
        TransportClient client = factory.getObject();
        this.releasable = client;
        return client;
    }


    public void destroy() throws Exception {
        if(this.releasable != null) {
            try {
                if(logger.isInfoEnabled()) {
                    logger.info("Closing Elasticsearch client");
                }

                try {
                    this.releasable.close();
                } catch (NoSuchMethodError var2) {
                    ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(Releasable.class, "release"), this.releasable);
                }
            } catch (Exception var3) {
                if(logger.isErrorEnabled()) {
                    logger.error("Error closing Elasticsearch client: ", var3);
                }
            }
        }

    }
}
